package net.cyue.jpa;

import jakarta.persistence.*;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;


public class JPAUtil {

    private static EntityManagerPool POOL;
    private static final boolean init = false;

    public static void init(
        String unitName,
        String url,
        String user,
        String password,
        String... managedClassNames
    ) {
        CYuePersistenceUnitInfoConfiguration configuration = new CYuePersistenceUnitInfoConfiguration(
            unitName,
            url,
            user,
            password
        );
        for (String className : managedClassNames) {
            configuration.managedClassNameList.add(className);
        }
        POOL = new EntityManagerPool(configuration);
    }
    public static void init(
        String unitName,
        String url,
        String user,
        String password,
        Class<?>... managedClass
    ) {
        String[] classNames = new String[managedClass.length];
        for (int i = 0; i < managedClass.length; i++) {
            classNames[i] = managedClass[i].getName();
        }
        JPAUtil.init(
            unitName,
            url,
            user,
            password,
            classNames
        );
    }
    public static void setPoolSize(int size) {
        JPAUtil.POOL.setSize(size);
    }

    public static void transaction(Consumer<EntityManager> work) {
        JPAUtil.POOL.transaction(work);
    }
    public static void transactionWrapper(Consumer<EntityManagerWrapper> work) {
        JPAUtil.POOL.transactionWrapper(work);
    }

    public static Object selectOne(String jpql, Object... parameters) {
        return JPAUtil.POOL.selectOne(jpql, parameters);
    }
    public static Object selectOne(String jpql, Map<String, ?> parameters) {
        return JPAUtil.POOL.selectOne(jpql, parameters);
    }
    public static List select(String jpql, Object... parameters) {
        return JPAUtil.POOL.select(jpql, parameters);
    }
    public static <T> List<T> select(Class<T> clz, String jpql, Object... parameters) {
        return (List<T>) JPAUtil.POOL.select(jpql, parameters);
    }
    public static List select(String jpql, Map<String, ?> parameters) {
        return JPAUtil.POOL.select(jpql, parameters);
    }
    public static <T> List<T> select(Class<T> clz, String jpql, Map<String, Object> parameters) {
        return (List<T>) JPAUtil.POOL.select(jpql, parameters);
    }
    public static int executeUpdate(String jpql, Object... parameters) {
        return JPAUtil.POOL.executeUpdate(jpql, parameters);
    }
    public static int executeUpdate(String jpql, Map<String, ?> parameters) {
        return JPAUtil.POOL.executeUpdate(jpql, parameters);
    }

    public static void persist(Object obj) {
        JPAUtil.POOL.persist(obj);
    }
    public static <T> T find(Class<T> clz, int id) {
        return JPAUtil.POOL.find(clz, id);
    }
    public static void remove(Object obj) {
        JPAUtil.POOL.remove(obj);
    }
    public static <T> void remove(Class<T> clz, int id) {
        JPAUtil.POOL.remove(clz, id);
    }
}
